package cn.xchats.rpc.client;
import cn.xchats.rpc.common.core.NettyDecoder;
import cn.xchats.rpc.common.core.TcpFuture;
import cn.xchats.rpc.common.dto.ResponseDto;
import cn.xchats.rpc.common.type.NettyType;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;


/*
 *
 *@author teddy
 *@date 2018/6/27
 */
public class TcpClientHander extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ResponseDto response;
        try {
            response = (ResponseDto)msg;//JSONObject.parseObject(decoder, ResponseDto.class);
        } catch (Exception e) {
            throw  new NullPointerException("client > > > > 解码错误");
        }
        //心跳
        if (NettyType.ALL_IDLE_MSG.equals(response.getBody())){
            //ctx.channel().writeAndFlush(NettyType.ALL_IDLE_MSG);
            return;
        }

        //添加返回值
        TcpFuture.recive(response);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
